<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\company\controller;

use think\Db;
use think\Exception;
use think\Request;

class Subscribe extends Common
{
    public function index()
    {
        $data = $map = [];
        //页码
        $pageNum = 1;
        if(Request::instance()->isPost()){
            $data = input('post.');
            if (!empty($data['start_time']) && !empty($data['end_time'])) {
                $map['date_date'] = ['between time', [$data['start_time'] , $data['end_time']]];
            } elseif (!empty($data['start_time']) && empty($data['end_time'])) {
                $map['date_date'] = ['>= time', $data['start_time']];
            } elseif (empty($data['start_time']) && !empty($data['end_time'])) {
                $map['date_date'] = ['<= time', $data['end_time']];
            }
            if (is_numeric(input('post.pageNum'))) {
                $pageNum = input('post.pageNum');
            }
        }
        $data['pageNum'] = $pageNum;
        //每页数量
        $numPerPage = config('num_per_page');
        $data['numPerPage'] = $numPerPage;
        $model = new \app\company\model\Subscribe();
        //列表数据
        $list = $model->getSubscribeList($pageNum, $numPerPage, $map);
        $data['list'] = $list;
        //数据总数
        $count = $model->getSubscribeCount($map);
        $data['count'] = $count;
        return view('', $data);
    }
    // 添加
    public function add()
    {
        $data = [];
        $data['company_id'] = $company_id = session('company.company_id');
        $data['time_state'] = db('time_state')->select();
        $data['minDate'] = db('date')->order('id')->limit(1)->value('date_date');
        $data['maxDate'] = db('date')->order('id desc')->limit(1)->value('date_date');
        $data['subscribe_num'] = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num');
        if(45 == $data['company_id']){
            $data['subscribe_num_gas'] = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num_gas');
            $data['subscribe_num_diesel'] = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num_diesel');
        }
        return view('', $data);
    }
    //执行添加
    public function addSubscribe()
    {
        $data = input('post.');
        $model = new \app\company\model\Subscribe();
        try {
            $model->doAdd_subscribe($data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '添加成功',
            'navTabId' => 'SubscribeManage',
            'callbackType' => 'closeCurrent',
        ]);
    }

    /**
     * 查看订单详情
     */
    public function detail()
    {
        $data = input('get.');
        $model = new \app\company\model\Subscribe();
        $data['company_id'] = $company_id = session('company.company_id');
        $data['subscribe_num'] =  Db::name('company')->where('id', '=', $company_id)->value('subscribe_num');
        if(45 == $data['company_id']){
            $data['subscribe_num_gas'] =  Db::name('company')->where('id', '=', $company_id)->value('subscribe_num_gas');
            $data['subscribe_num_diesel'] =  Db::name('company')->where('id', '=', $company_id)->value('subscribe_num_diesel');
        }
        $data['subscribe'] = $model->getDetail($data);
        return view('', $data);
    }

    //执行修改
    public function editSubscribe()
    {
        $data = input('post.');
        $model = new \app\company\model\Subscribe();
        try {
            $model->doEdit_subscribe($data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '修改成功',
            'navTabId' => 'SubscribeManage',
            'callbackType' => 'closeCurrent',
        ]);
    }

    // 执行删除
    public function doDelete()
    {
        $userId = input('get.id');
        $Project = new \app\company\model\Subscribe();
        try {
            $Project->doDelete($userId);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '删除成功',
            'navTabId' => 'SubscribeManage',
        ]);
    }

    public function insertData()
    {
        $company_id = 51;
        for ($i = 22; $i <= 30;$i++){
            $sql = "INSERT INTO `cd_subscribe`(`date_id`, `company_id`, `time_state_id`, `num`) VALUES 
                ({$i}, {$company_id}, 1, 0),
                ({$i}, {$company_id}, 2, 0),
                ({$i}, {$company_id}, 3, 0),
                ({$i}, {$company_id}, 4, 0),
                ({$i}, {$company_id}, 5, 0),
                ({$i}, {$company_id}, 6, 0),
                ({$i}, {$company_id}, 7, 0),
                ({$i}, {$company_id}, 8, 0);";
            $res = Db::execute($sql);
            echo '成功：' . $res . PHP_EOL;
        }
    }

    public function num()
    {
        $data['company_id'] = $company_id = session('company.company_id');
        $data['subscribe_num'] = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num');
        if(45 == $data['company_id']){
            $data['subscribe_num_gas'] = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num_gas');
            $data['subscribe_num_diesel'] = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num_diesel');
        }
        return view('', $data);
    }

    /**
     * @Desc 编辑默认台数
     * @return \think\response\Json
     */
    public function editNum()
    {
        $company_id = session('company.company_id');
        $subscribe_num = input('post.subscribe_num/d'); // 修改后的默认台数:10
        $old_num = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num'); // 原来的默认台数:4
        Db::name('company')->where('id', '=', $company_id)->setField('subscribe_num', $subscribe_num);
        // 修改后的大于原来的
        if ($subscribe_num > $old_num) {
            $num = $subscribe_num - $old_num; // 6 - 4 = 2
            Db::name('subscribe')->where('company_id', '=', $company_id)->where('date_id', '>=', static function ($query) {
                $query->name('date')->field('id')->where('date_date', '=', date('Y-m-d'));
            })->inc('num', $num)->update();
            // 修改后的小于原来的
        } elseif ($subscribe_num < $old_num) {
            $num = $old_num - $subscribe_num; // 6 - 4 = 2
            Db::name('subscribe')->where('company_id', '=', $company_id)->where('date_id', '>=', static function ($query) {
                $query->name('date')->field('id')->where('date_date', '=', date('Y-m-d'));
            })->update([
                'num' => Db::raw("IF(num < {$num}, 0, num - {$num})"),
            ]);
        }
        if (45 == $company_id) {
            $subscribe_num_gas = input('post.subscribe_num_gas/d'); // 10
            $old_num_gas = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num_gas'); // 5
            Db::name('company')->where('id', '=', $company_id)->setField('subscribe_num_gas', $subscribe_num_gas);
            if ($subscribe_num_gas > $old_num_gas) {
                $num_gas = $subscribe_num_gas - $old_num_gas;
                Db::name('subscribe')->where('company_id', '=', $company_id)->where('date_id', '>', static function ($query) {
                    $query->name('date')->field('id')->where('date_date', '=', date('Y-m-d'));
                })->inc('num_gas', $num_gas)->update();
            } elseif ($subscribe_num_gas < $old_num_gas) {
                $num_gas = $old_num_gas - $subscribe_num_gas;
                Db::name('subscribe')->where('company_id', '=', $company_id)->where('date_id', '>', static function ($query) {
                    $query->name('date')->field('id')->where('date_date', '=', date('Y-m-d'));
                })->update([
                    'num_gas' => Db::raw("IF(num_gas < {$num_gas}, 0, num_gas - {$num_gas})"),
                ]);
            }
            $subscribe_num_diesel = input('post.subscribe_num_diesel/d'); // 10
            $old_num_diesel = Db::name('company')->where('id', '=', $company_id)->value('subscribe_num_diesel'); // 5
            Db::name('company')->where('id', '=', $company_id)->setField('subscribe_num_diesel', $subscribe_num_diesel);
            if ($subscribe_num_diesel > $old_num_diesel) {
                $num_diesel = $subscribe_num_diesel - $old_num_diesel;
                Db::name('subscribe')->where('company_id', '=', $company_id)->where('date_id', '>', static function ($query) {
                    $query->name('date')->field('id')->where('date_date', '=', date('Y-m-d'));
                })->inc('num_diesel', $num_diesel)->update();
            } elseif ($subscribe_num_diesel < $old_num_diesel) {
                $num_diesel = $old_num_diesel - $subscribe_num_diesel;
                Db::name('subscribe')->where('company_id', '=', $company_id)->where('date_id', '>', static function ($query) {
                    $query->name('date')->field('id')->where('date_date', '=', date('Y-m-d'));
                })->update([
                    'num_diesel' => Db::raw("IF(num_diesel < {$num_diesel}, 0, num_diesel - {$num_diesel})"),
                ]);
            }
        }
        return json([
            'statusCode'   => 200,
            'message'      => '修改成功',
            'navTabId'     => 'SubscribeNumManage',
            'callbackType' => 'closeCurrent',
        ]);
    }
}